/*
 * InvActivity displays a list of fire extinguishers within a specified building
 */
package com.example.wmuqrreader;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class InvActivity extends Activity {
	
	DatabaseHandler db;
	String building_id;
	
	ListView list;
	
	Button button_new;
	Button button_scan;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_inv_list);
		
		building_id = getIntent().getExtras().getString("BUILDING");
		
		Log.d("GOT: ", "BUILDING ID " + building_id);
		
		button_new 		= (Button) findViewById(R.id.btn_new);   
		button_scan 	= (Button) findViewById(R.id.btn_scan);
		
		db 				= new DatabaseHandler(this);
		list 			= (ListView) findViewById(R.id.listView1);
		
        List<Inventory> inventory = new ArrayList<Inventory>();
        inventory = db.getAllInventory(Integer.parseInt(building_id)); 

        CustomInventoryListViewAdapter adapter = new CustomInventoryListViewAdapter(this, R.layout.simple_list_inventory, inventory);
        
        //Add a function for the EDIT row click
        list.setAdapter(adapter);       
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> av, View v, int pos, long id) {
            	Intent intent = new Intent();
            	
            	//Fetch the ID of the entry we just grabbed from the ListView & set as extra param to pass to next view
            	View rowItem 	= (View) av.getChildAt(pos);
            	intent.putExtra("ID", (String) ((TextView) rowItem.findViewById(R.id.textView_id)).getText());
				
				intent.setClass(InvActivity.this, WideActivity.class);
		        startActivityForResult(intent, 0);
            }
        });
           
        //Add a function for the NEW button click          
        button_new.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				
				intent.putExtra("BUILDING", building_id);
				
				intent.setClass(InvActivity.this, WideActivity.class);
		        startActivityForResult(intent, 0);		        
			}
		});
        
        //Add a function for the SCAN button click
        button_scan.setOnClickListener(new View.OnClickListener() {
                        
                @Override
                public void onClick(View v) {
                        Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                        intent.setPackage("com.google.zxing.client.android");
                        intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                        startActivityForResult(intent, 1);                     
                }
         });

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present
		getMenuInflater().inflate(R.menu.inv_list, menu);
		return true;
	}
	
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == 0) 
        {
        	if (resultCode == RESULT_OK) 
            {
        		//Refresh the Inventory List activity to display the updated data     		
        		Intent refresh = new Intent();        		
        		refresh.setClass(this, InvActivity.class);
        		
        		refresh.putExtra("BUILDING", building_id);
        		
        		startActivity(refresh);
        		this.finish();
            }
        }
        
        if (requestCode == 1 ) 
        {
            if (resultCode == RESULT_OK) 
            {
                String contents = intent.getStringExtra("SCAN_RESULT");
                
                //TODO: Handle successful scan  
                //editText_inv.setText(contents);
                //populateFields(inventory_num);
                
                //finish();
            } 
            else if (resultCode == RESULT_CANCELED) 
            {
                // Handle cancel
            }
        }
    }

}
